package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Brand;
import com.grom.po.Customer;
import com.grom.po.Store;
import com.grom.service.BrandService;
import com.grom.service.CustomerService;
import com.grom.service.StoreService;
import com.grom.util.JsonUtil;
import com.grom.util.SecurityUtil;

public class QueryAction {
	public void gotoQuery() {
		List<Customer> allCustomer = CustomerService.instance.getAllCustomer();
		allCustomer = getUserBindCustomer(allCustomer);
		ContextHolder.setRequestAttribute("allCustomer", allCustomer);
		ContextHolder.forward("/WEB-INF/jsp/query/query_new.jsp");
	}

	public void doQuery() {
		String cusType_s = ContextHolder.findInstance().getRequest()
				.getParameter("cusType_sh");
		String userId = ContextHolder.findInstance().getRequest()
				.getParameter("userId_sh");
		String brandName = ContextHolder.findInstance().getRequest()
				.getParameter("brandName_sh");
		String sql = "select * from customer where ";
		if (userId != null && userId.trim().length() > 0) {
			sql += " userid ='" + userId + "' ";
		} else {
			sql += " 1=1 ";
		}
		if (brandName != null && brandName.trim().length() > 0) {
			sql += " and id in (select customerid from brand where brandName like '%"
					+ brandName + "%') ";
		} else {
			sql += "and 1=1 ";
		}
		if (cusType_s != null && cusType_s.trim().length() > 0) {
			sql += " and  custype = '" + cusType_s + "'";
		} else {
			sql += "and 1=1";
		}
		List<Customer> customers = CustomerService.instance
				.selectByCondition(sql);
		customers = getUserBindCustomer(customers);
		ContextHolder.setRequestAttribute("allCustomer", customers);
		ContextHolder.forward("/WEB-INF/jsp/query/query_new.jsp");
	}

	public void getAllBrand() {
		String cusId = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		String sql = "select * from brand where customerid = '" + cusId + "'";
		List<Brand> brands = BrandService.instance.selectByCondition(sql);
		String json = JsonUtil.parseToJson(brands);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("brands=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getAllStore() {
		String cusId = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		String sql = "select * from store where customerid = '" + cusId + "'";
		List<Store> stores = StoreService.instance.conditionSelect(sql);
		String json = JsonUtil.parseToJson(stores);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("stores=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private List<Customer> getUserBindCustomer(List<Customer> customers) {
		List<Customer> result = new ArrayList<Customer>();
		for (int i = 0; i < customers.size(); i++) {
			if (SecurityUtil.isUserHasRight(customers.get(i).getUserId())) {
				result.add(customers.get(i));
			}
		}
		return result;
	}
}
